package models

import "github.com/beego/beego/v2/client/orm"

type User struct {
	Id      int
	Name    string
	AddTime int64
	Status  int
	Mobile  string
	Avatar  string
}

func init() {
	orm.RegisterModel(new(User))
}

func UserInfo(id int) (User, error) {
	o := orm.NewOrm()
	user := User{Id: id}
	err := o.Read(&user)
	return user, err
}

func SqlUserInfo(id int) (User, error) {
	o := orm.NewOrm()
	user := User{}
	err := o.Raw("select `name`, `mobile` from user where id = ? limit 1", id).QueryRow(&user)
	return user, err
}

func UserSave(name, mobile, avatar string) error {
	o := orm.NewOrm()
	user := new(User)
	user.Name = name
	user.Mobile = mobile
	user.Avatar = avatar
	user.Status = 0
	_, err := o.Insert(user)
	return err
}

func SqlUserSave(name, mobile, avatar string) error {
	o := orm.NewOrm()
	_, err := o.Raw("insert into user (`name`, `mobile`, `avatar`, `status`) values(?, ?, ?, ?)", name, mobile, avatar, 0).Exec()
	return err
}

func SqlUserUpdate(id int, name string) error {
	o := orm.NewOrm()
	_, err := o.Raw("update user set name = ? where id = ?", name, id).Exec()
	return err
}

func SqlUserDelete(id int) error {
	o := orm.NewOrm()
	_, err := o.Raw("delete from user where id = ?", id).Exec()
	return err
}

func SqlUserList() (int64, []User, error) {
	o := orm.NewOrm()
	var users []User
	num, err := o.Raw("select name, mobile from user").QueryRows(&users)
	return num, users, err
}
